package test.test1;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import test.test1.mybatis.mapping.UserMapper;
import test.test1.mybatis.pojo.MyClass;
import test.test1.mybatis.pojo.Student;
import test.test1.mybatis.pojo.User;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        String resource="mybatis_conf.xml";
        InputStream is=App.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(is);
        SqlSession session =sessionFactory.openSession();
//        String statement ="test.test1.mybatis.mapping.userMapper.getALLUsers";
//        
//        List<User> users =session.selectList(statement);
//        for (User user : users) {
//			System.out.println(user);
//		}
//       statement ="test.test1.mybatis.mapping.userMapper.getUserAttrMapById";
//       Map<String,Object> user =session.selectOne(statement,1);
//       for(String key:user.keySet()){
//    	   System.out.println("key="+key+",value="+user.get(key)+",vt="+user.get(key).getClass().getTypeName());
//       }
       
//       User user =new User();
//       user.setName("jirry");
//       user.setCode("u4");
//       user.setPassword("125");
//       addNewUser(session, user);
//       session.close();
//        String statement ="test.test1.mybatis.mapping.studentMapper.getStudentById";
//        Student stu=session.selectOne(statement, 1);
//        System.out.println(stu.getMyClass().getName());
//        
//        getMyClass(session);
//        System.out.println();
//
//        Student search =new Student();
//        getStudentNameLike(session, search);
//        System.out.println();
//        search.setName("");
//        getStudentNameLike(session, search);
//        System.out.println();
//
//        search.setName("%12%");
//        getStudentNameLike(session, search);
       // String st ="test.test1.mybatis.mapping.studentMapper.getStudentByConditions";
//        List<Integer> list=new ArrayList<Integer>();
//        list.add(1);
//        list.add(2);
//        List<Student> students=session.selectList(st, list);
//        for (Student student : students) {
//			System.out.println(student);
//		}
//        
//        Student stu=new Student();
//        stu.setName("stlname");
//        List<Student> students =session.selectList(st,stu);
//        for (Student student : students) {
//			System.out.println(student);
//		}
//    	String st ="test.test1.mybatis.mapping.studentMapper.findStudentLike";
//    	Student student=new Student();
//    	student.setName("stlname");
//    	//student.setClassId(1);
//    	 List<Student> students =session.selectList(st,student);
//    	 for (Student stu : students) {
// 			System.out.println(stu);
// 		}
       // callSP(session);
        UserMapper ium =session.getMapper(UserMapper.class);
        List<User> users =ium.getALLUsers();
        for (User user : users) {
			System.out.println(user);
		}
        
        User user =ium.getUserById(1);
        System.out.println("getUserById:"+user);
    }
    
    private static void addNewUser(SqlSession session,User user){
    	String statement="test.test1.mybatis.mapping.userMapper.insertUser";
    	System.out.println("before="+user);
    	
    	int count=session.insert(statement,user);
    	session.commit();
    	System.out.println("insert count="+count);
    	System.out.println("after="+user);

    }
    
    private static void getMyClass(SqlSession session){
    	String st="test.test1.mybatis.mapping.myClassMapper.getClassById";
    	MyClass mc=session.selectOne(st,1);
    	System.out.println(mc);
    	for(Student stu:mc.getStudents()){
    		System.out.println(stu);
    	}
    }
    
    private static void getStudentNameLike(SqlSession session, Student search){
    	String st ="test.test1.mybatis.mapping.studentMapper.getStudentNamelike";
    	List<Student> students =session.selectList(st,search);
    	for (Student student : students) {
			System.out.println(student);
		}
    }
    private static void callSP(SqlSession session){
    	String st="test.test1.mybatis.mapping.studentMapper.callSPtest";
    	Map<String,Integer> map =new HashMap<String, Integer>();
    	map.put("addend1", 2);
    	map.put("addend2", 5);
    	session.update(st,map);
    	System.out.println(map.get("theSum"));
    }
 
}
